/**
 * com.future CO.,ltd.
 */

package com.future.springcloud.controller;

import com.future.springcloud.entities.Payment;
import com.future.springcloud.entities.util.CommonResult;
import com.future.springcloud.service.IPaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.HttpRequestHandlerServlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 类说明：
 */
@Controller
@RequestMapping(value = "/payment")
public class PaymentController {

    @Value("${server.port}")
    private String serverPort;

    @Autowired
    private IPaymentService paymentService;

    @Autowired
    private DiscoveryClient discoveryClient;


    @GetMapping(value = "/test")
    @ResponseBody
    public CommonResult<String> test() {
        System.out.println("future");
        return new CommonResult<>(10, "future");
    }

    @PostMapping(value = "/create")
    @ResponseBody
    public CommonResult<Integer> create(@RequestBody Payment payment) {

        System.out.println("serserPort:" + serverPort);
        Integer i = paymentService.create(payment);

        return new CommonResult<Integer>(10, i);

    }

    @GetMapping(value = "/get/{id}")
    @ResponseBody
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        String cookie = request.getHeader("Cookie");
        System.out.println("token:"+token);
        System.out.println("cookie:"+cookie);
        Payment paymentById = paymentService.getPaymentById(id);
        return new CommonResult<>(10, "OK serverPort" + serverPort, paymentById);
    }

    @GetMapping(value = "/discovery")
    @ResponseBody
    public Object discovery() {
        List<String> services = discoveryClient.getServices();
        for (String element : services
                ) {
            System.out.println(element);
        }
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance element : instances
                ) {
            System.out.println(element.getServiceId() + "\t"
                    + element.getHost() + "\t"
                    + element.getPort() + "\t"
                    + element.getUri()
            );
        }
        return this.discoveryClient;
    }

    @GetMapping(value = "/lb")
    @ResponseBody
    public String getPaymentLB(){

        return serverPort;
    }
}
